let Rule = require("../rule.js")
class MaxRule extends Rule{
	name(){
		return 'max'
	}
	check(column,value,values,params){
		if(typeof parseInt(params[0]) !== 'number'){
			console.error("min的校验规则为[min: number]")
			return false
		}
		let length = parseInt(params[0])
		if(typeof value === 'string'){
			return value.length <= length
		}
		var re = /^[1-9]\d*$/;
		if(re.test(value)){
			return value <= length
		}
		if(Array.isArray(value)){
			return value.length <= length
		}
		return false
	}
	message(column,value,values,messages,attribute,params){
		let type = "string"
		var re = /^[1-9]\d*$/;
		if(typeof value !== 'string' && re.test(value)){
			type = 'numeric'
		}
		if(Array.isArray(value)){
			type = 'array'
		}
		let message = messages[type]
		message = message?message.replace(":attr",attribute === 'undefined'?column:attribute):"未找到对应信息"
		return message.replace(":max",params[0])
	}
}
module.exports = MaxRule